package com.ruoyi.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.base.domain.BaseBsBd;
import com.ruoyi.base.mapper.BaseBsBdMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.base.domain.bo.BaseBsProjectBo;
import com.ruoyi.base.domain.vo.BaseBsProjectVo;
import com.ruoyi.base.domain.BaseBsProject;
import com.ruoyi.base.mapper.BaseBsProjectMapper;
import com.ruoyi.base.service.IBaseBsProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目管理Service业务层处理
 *
 * @author zyb
 * @date 2024-09-24
 */
@RequiredArgsConstructor
@Service
public class BaseBsProjectServiceImpl implements IBaseBsProjectService {

    private final BaseBsProjectMapper baseMapper;
    private final BaseBsBdMapper bdMapper;

    /**
     * 查询项目管理
     */
    @Override
    public BaseBsProjectVo queryById(Long projectId){
        return baseMapper.selectVoById(projectId);
    }

    /**
     * 查询项目管理列表
     */
    @Override
    public TableDataInfo<BaseBsProjectVo> queryPageList(BaseBsProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseBsProject> lqw = buildQueryWrapper(bo);
        Page<BaseBsProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目管理列表
     */
    @Override
    public List<BaseBsProjectVo> queryList(BaseBsProjectBo bo) {
        LambdaQueryWrapper<BaseBsProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseBsProject> buildQueryWrapper(BaseBsProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseBsProject> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProjectType()), BaseBsProject::getProjectType, bo.getProjectType());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectCatalog()), BaseBsProject::getProjectCatalog, bo.getProjectCatalog());
        lqw.eq(bo.getProjectAmout() != null, BaseBsProject::getProjectAmout, bo.getProjectAmout());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), BaseBsProject::getProjectName, bo.getProjectName());
        lqw.eq(bo.getDeptId() != null, BaseBsProject::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), BaseBsProject::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectLeader()), BaseBsProject::getProjectLeader, bo.getProjectLeader());
        lqw.eq(bo.getPlanBeginData() != null, BaseBsProject::getPlanBeginData, bo.getPlanBeginData());
        lqw.eq(bo.getPlanEndData() != null, BaseBsProject::getPlanEndData, bo.getPlanEndData());
        lqw.eq(bo.getFactBeginData() != null, BaseBsProject::getFactBeginData, bo.getFactBeginData());
        lqw.eq(bo.getFactEndData() != null, BaseBsProject::getFactEndData, bo.getFactEndData());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectStatus()), BaseBsProject::getProjectStatus, bo.getProjectStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovalStatus()), BaseBsProject::getApprovalStatus, bo.getApprovalStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectCode()), BaseBsProject::getProjectCode, bo.getProjectCode());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectGeneral()), BaseBsProject::getProjectGeneral, bo.getProjectGeneral());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectSacle()), BaseBsProject::getProjectSacle, bo.getProjectSacle());
        lqw.eq(bo.getBidPrice() != null, BaseBsProject::getBidPrice, bo.getBidPrice());
        lqw.eq(bo.getControlPrice() != null, BaseBsProject::getControlPrice, bo.getControlPrice());
        lqw.eq(bo.getTaxRate() != null, BaseBsProject::getTaxRate, bo.getTaxRate());
        lqw.eq(bo.getShigongCompanyId() != null, BaseBsProject::getShigongCompanyId, bo.getShigongCompanyId());
        lqw.eq(bo.getJianliCompanyId() != null, BaseBsProject::getJianliCompanyId, bo.getJianliCompanyId());
        lqw.eq(bo.getShenjiCompanyId() != null, BaseBsProject::getShenjiCompanyId, bo.getShenjiCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), BaseBsProject::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getPostpone()), BaseBsProject::getPostpone, bo.getPostpone());
        return lqw;
    }

    /**
     * 新增项目管理
     */
    @Override
    public Boolean insertByBo(BaseBsProjectBo bo) {
        BaseBsProject add = BeanUtil.toBean(bo, BaseBsProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectId(add.getProjectId());
        }
        return flag;
    }

    /**
     * 修改项目管理
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(BaseBsProjectBo bo) {
        BaseBsProject update = BeanUtil.toBean(bo, BaseBsProject.class);
        validEntityBeforeSave(update);
        boolean re = baseMapper.updateById(update) > 0;
        //同步修改标段的所属项目名称；
        BaseBsBd bd = new BaseBsBd();
        bd.setProjectName(bo.getProjectName());
        bdMapper.update(bd,new LambdaQueryWrapper<BaseBsBd>().eq(BaseBsBd::getProjectId,bo.getProjectId()));
        return re;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseBsProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
